#include <iostream>
#include <string>
#include <map>

using namespace std;

map<string, bool> prim;
int d[200001], res = -1;

int main()
{
	while (true)
	{
		string s;
		cin >> s;
		if (s == '.') break;
		prim[s] = true;
	}

	string S, pre;
	while (cin >> pre) S += pre;

	memset(d, false, sizeof(d));
	d[0] = true;
	S = '*' + S;
	for (int i = 1; i < S.size(); i++)
	{
		string pre = "";
		for (int j = 1; j < 10; j++)
		{
			int start = i - j;
			if (start < 0) break;
			pre = S[start+1] + pre;
			cout << pre << endl;
			if (d[start]) 
				if (prim.find(pre) != prim.end())
				{
					d[i] = true;
					break;
				}
		}
		if (d[i]) res = i;
	}

	cout << res;

	return 0;
}
